import 'package:flutter/material.dart';
import 'package:flutter_community/pages/home/detail.dart';
import 'package:flutter_community/pages/house/build_list.dart';
import 'package:flutter_community/pages/house/house_detail.dart';
import 'package:flutter_community/pages/house/house_form.dart';
import 'package:flutter_community/pages/house/house_list.dart';
import 'package:flutter_community/pages/house/location_list.dart';
import 'package:flutter_community/pages/house/room_list.dart';
import 'package:flutter_community/pages/login/index.dart';
import 'package:flutter_community/pages/profile/index.dart';
import 'package:flutter_community/pages/tabBar.dart';
import 'package:flutter_community/stores/counter.dart';
import 'package:flutter_community/utils/preference.dart';
import 'package:flutter_community/utils/showToast.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(
    MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (context) => CounterModel()),
        ],
        child: MaterialApp(
          routes: {
            '/': (context) => const MyTabBar(),
            '/detail': (context) => const DetailPage(),
            '/login': (context) => const LoginPage(),
          },
          initialRoute: '/',
          //统一token判断是否跳转登录页
          onGenerateRoute: (settings) {
            //获取token
            final myToken = TokenManager();
            final token = myToken.getToken() ?? '';
            if (token.isEmpty && settings.name != '/login') {
              return MaterialPageRoute(builder: (context) => const LoginPage());
            }
            //个人信息
            if (settings.name == '/profile') {
              return MaterialPageRoute(
                  builder: (context) => ProfilePage(
                        userInfo: settings.arguments as Map,
                      ));
            }
            //房屋列表
            if (settings.name == '/houseList') {
              return MaterialPageRoute(
                  builder: (context) => const HouseList(), settings: settings);
            }
            //添加房屋
            if (settings.name == '/location_list') {
              return MaterialPageRoute(
                  builder: (context) => const LocationList());
            }
            //选择楼栋
            if (settings.name == '/build_list') {
              return MaterialPageRoute(
                  builder: (context) =>
                      BuildList(point: (settings.arguments as Map)['point']));
            }

            //选择房间
            if (settings.name == '/room_list') {
              return MaterialPageRoute(
                  builder: (context) => RoomList(
                        point: (settings.arguments as Map)['point'],
                        building: (settings.arguments as Map)['building'],
                      ));
            }
            //添加房屋信息
            if (settings.name == '/house_form') {
              return MaterialPageRoute(
                  builder: (context) => HouseForm(
                        params: (settings.arguments as Map),
                      ));
            }
            //房屋详情
            if (settings.name == '/house_detail') {
              return MaterialPageRoute(
                  builder: (context) => HouseDetail(
                        houseInfo: settings.arguments as Map,
                      ));
            }

            //未匹配到跳转到登录页
            return MaterialPageRoute(builder: (context) => const LoginPage());
          },
        )),
  );
}
